package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/Unknwon/com"
	"hytx_manager/models"
	"hytx_manager/pkg/app"
	"net/http"
	"hytx_manager/pkg/e"
	"strconv"
	"hytx_manager/pkg/setting"
	"github.com/astaxie/beego/validation"
)

func PartnerList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	data, paginate := models.PartnerList(page, limit, c)
	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})

}

func PartnerGet(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()

	item := models.PartnerByIdResult{ID: id}
	data, err := item.PartnerById()
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "查找成功！", data)
}

func PartnerAdd(c *gin.Context) {
	appG := app.Gin{c}
	name := c.PostForm("name")
	user_id := com.StrTo(c.PostForm("user_id")).MustInt()
	id_card := c.PostForm("id_card")
	id_card_img := c.PostForm("id_card_img")
	bank_card_no := c.PostForm("bank_card_no")
	bank_card_name := c.PostForm("bank_card_name")
	bank_name := c.PostForm("bank_name")
	province_id := com.StrTo(c.PostForm("province_id")).MustInt()
	operator_id := com.StrTo(c.PostForm("operator_id")).MustInt()
	city_id := com.StrTo(c.PostForm("city_id")).MustInt()
	area_id := com.StrTo(c.PostForm("area_id")).MustInt()
	phone := c.PostForm("phone")
	partner := models.Partner{
		Name:         name,
		UserId:       user_id,
		IdCard:       id_card,
		IdCardImg:    id_card_img,
		BankCardNo:   bank_card_no,
		BankCardName: bank_card_name,
		BankName:     bank_name,
		ProvinceId:   province_id,
		CityId:       city_id,
		AreaId:       area_id,
		OperatorId:   operator_id,
		Phone:        phone,
	}
	err := partner.AddPartner()
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "添加成功！", "")
}

func DowngradePartner(c *gin.Context) {
	appG := app.Gin{c}
	user_id := com.StrTo(c.Query("user_id")).MustInt()

	err := models.DowngradePartner(user_id)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "添加成功！", "")
}
func PartnerEdit(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	name := c.PostForm("name")
	user_id := com.StrTo(c.PostForm("user_id")).MustInt()
	id_card := c.PostForm("id_card")
	id_card_img := c.PostForm("id_card_img")
	bank_card_no := c.PostForm("bank_card_no")
	bank_card_name := c.PostForm("bank_card_name")
	bank_name := c.PostForm("bank_name")
	province_id := com.StrTo(c.PostForm("province_id")).MustInt()
	operator_id := com.StrTo(c.PostForm("operator_id")).MustInt()
	city_id := com.StrTo(c.PostForm("city_id")).MustInt()
	area_id := com.StrTo(c.PostForm("area_id")).MustInt()
	phone := c.PostForm("phone")

	partner := models.Partner{
		ID:           id,
		Name:         name,
		UserId:       user_id,
		IdCard:       id_card,
		IdCardImg:    id_card_img,
		BankCardNo:   bank_card_no,
		BankCardName: bank_card_name,
		BankName:     bank_name,
		ProvinceId:   province_id,
		CityId:       city_id,
		AreaId:       area_id,
		OperatorId:   operator_id,
		Phone:        phone,
	}
	err := partner.PartnerEdit()

	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "修改成功！", "")
}
func PartnerDel(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("必须大于1")
	partner := models.Partner{ID: id}
	err := partner.PartnerDel()
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "删除成功！", "")
}

func IsEnabledPartner(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("user_id")).MustInt()
	status,_ := strconv.Atoi(c.Query("state"))
	valid := validation.Validation{}
	valid.Min(id, 1, "user_id").Message("必须大于1")
	models.DB.Model(&models.Partner{}).Where("id=?", id).Update("status", status)
	var p models.Partner
	models.DB.Model(models.Partner{}).Where("id=?", id).First(&p)
	err := models.UpdateIsEnabled(p.UserId, status)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "修改成功！", "")

}

func ExaminePartner(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	status := c.Query("status")
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("必须大于1")
	err := models.ExaminePartner(id, status)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "修改成功！", "")
}

func GetOperatorList(c *gin.Context) {
	appG := app.Gin{c}
	var operators []*models.Operators
	err := models.DB.Table("operators").Select("*").Find(&operators).Error
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	appG.ResponseMsg(200, "成功！", operators)
}

func GetMemberList(c *gin.Context) {
	appG := app.Gin{c}
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	var memberList []*models.MemberInfoList

	item := models.DB.Table("users as t1").Select("t1.id, t2.user_name, t2.company, t1.phone").Joins("left join cards as t2 on t1.id = t2.user_id").Where("t1.type=4").Where("t1.phone <> ''").Find(&memberList)

	if name := c.Query("name"); name != "" {
		item = item.Where("t1.phone like ? or t2.user_name like ?", "%"+name+"%", "%"+name+"%")
	}
	var count int
	item.Count(&count)
	paginate := models.NewPage(page, limit, count)
	err := item.Offset(paginate.Offset).Limit(paginate.Limit).Find(&memberList).Error
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"paginate": paginate,
			"data":     memberList,
		},
	})

}
func PartnerAble(c *gin.Context) {

}
func PartnerOperatorList(c *gin.Context) {

}
